<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>goog.editor.plugins.LinkShortcutPlugin Tests</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.dom.Range');
  goog.require('goog.editor.plugins.BasicTextFormatter');
  goog.require('goog.editor.plugins.LinkShortcutPlugin');
  goog.require('goog.editor.plugins.LinkBubble');
  goog.require('goog.editor.Field');
  goog.require('goog.events.KeyCodes');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.dom');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
  <div id="cleanup">
    <div id="field">http://www.google.com/</div>
  </div>
  <script>
    var propertyReplacer;

    function setUp() {
      propertyReplacer = new goog.testing.PropertyReplacer();
    }

    function tearDown() {
      propertyReplacer.reset();
      var field = document.getElementById('cleanup');
      goog.dom.removeChildren(field);
      field.innerHTML = '<div id="field">http://www.google.com/</div>';
    }

    function testShortcutCreatesALink() {
      propertyReplacer.set(window, 'prompt', function() {
        return 'http://www.google.com/'; });
      var linkBubble = new goog.editor.plugins.LinkBubble();
      var formatter = new goog.editor.plugins.BasicTextFormatter();
      var plugin = new goog.editor.plugins.LinkShortcutPlugin();
      var fieldEl = document.getElementById('field');
      var field = new goog.editor.Field('field');
      field.registerPlugin(formatter);
      field.registerPlugin(linkBubble);
      field.registerPlugin(plugin);
      field.makeEditable();
      field.focusAndPlaceCursorAtStart();
      var textNode = goog.testing.dom.findTextNode('http://www.google.com/',
          fieldEl);
      goog.testing.events.fireKeySequence(
          field.getElement(), goog.events.KeyCodes.K, { ctrlKey: true });

      var href = field.getElement().getElementsByTagName('A')[0];
      assertEquals('http://www.google.com/', href.href);
      var bubbleLink =
          document.getElementById(goog.editor.plugins.LinkBubble.TEST_LINK_ID_);
      assertEquals('http://www.google.com/', bubbleLink.innerHTML);
    }
  </script>
</body>
</html>
